﻿/* Author:
 *  Andrea Vandin
 */
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CompactFramework.Utilities.Localization;
using TripLOAMobile.WS_GeoRef;

namespace TripLOAMobile
{
    /// <summary>
    /// This form is used to disambiguate a manually inserted address.
    /// </summary>
    public partial class DisplayAlternativeAddresses : LocalizedForm
    {
        private string latLongSelected;
        private bool setted = false;
        public double LatSelected
        {
            get;
            set;
        }
        /// <summary>
        /// Gets or sets the Longitude of the alternative selected.
        /// </summary>
        /// <value>The Longitude of the alternative selected.</value>
        public double LngSelected
        {
            get;
            set;
        }


        /// <summary>
        /// Gets a value indicating whether an alternative is selected.
        /// </summary>
        /// <value><c>true</c> if selected; otherwise, <c>false</c>.</value>
        public bool Selected
        {
            get
            {
                return setted;
            }
        }

        /// <summary>
        /// Gets or sets the latitude and the longitude of the alternative selected.
        /// </summary>
        /// <value>The latitude and longitude selected.</value>
        public string LatLongSelected
        {
            get
            {
                if (setted)
                    return latLongSelected;
                else 
                    return null;
            }
            set
            {
                latLongSelected = value;
                setted = true;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DisplayAlternativeAddresses"/> class.
        /// </summary>
        public DisplayAlternativeAddresses()
        {
            InitializeComponent();
            this.AddMenusToDictionary();
            UpdateResources();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DisplayAlternativeAddresses"/> class.
        /// </summary>
        /// <param name="listOfAlternatives">The list of alternatives.</param>
        public DisplayAlternativeAddresses(GeoLoc[] listOfAlternatives)
        {
            InitializeComponent();
            listToDisplay.View = View.Details;
            listToDisplay.FullRowSelect = true;
            listToDisplay.CheckBoxes = false;
            listToDisplay.Columns.Add("", 1, HorizontalAlignment.Left);
            listToDisplay.Columns.Add("Alternative Addresses", 240, HorizontalAlignment.Left);

            if ((listOfAlternatives != null))
            {
                ListViewItem item;
                foreach (GeoLoc g in listOfAlternatives)
                {
                    item = new ListViewItem();
                    item.SubItems.Add(g.Address);
                    item.SubItems.Add(g.Description);
                    item.SubItems.Add(g.Lat.ToString());
                    item.SubItems.Add(g.Lng.ToString());
                    listToDisplay.Items.Add(item);
                }
            }
            this.AddMenusToDictionary();
            UpdateResources();
        }

        /// <summary>
        /// The subclasses of the LocalizedForm should override this method and use this method to
        /// associate all menu items with custom names with the help of <see cref="AddMenuToDictionary"/>
        /// method.
        /// </summary>
        private void AddMenusToDictionary()
        {
            this.AddMenuToDictionary(this.menuLeft, "menuLeft");
            this.AddMenuToDictionary(this.menuRight, "menuRight");
            this.AddMenuToDictionary(this.menuItem3, "menuBack");
            this.AddMenuToDictionary(this.menuItem4, "menuHome");
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the listToDisplay control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void listToDisplay_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.menuRight.Enabled = true;
        }


        /// <summary>
        /// Handles the Click event of the menuRight control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void menuRight_Click(object sender, EventArgs e)
        {
            ListViewItem item = listToDisplay.Items[listToDisplay.SelectedIndices[0]];
            this.LatLongSelected = item.SubItems[3].Text + "," + item.SubItems[4].Text;
            LatSelected = Convert.ToDouble(item.SubItems[3].Text);
            LngSelected = Convert.ToDouble(item.SubItems[4].Text);
            Welcome.formBuffer.Close();

        }

        /// <summary>
        /// Handles the Click event of the menuItem3 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void menuItem3_Click(object sender, EventArgs e)
        {
            Welcome.formBuffer.Close();
        }

        /// <summary>
        /// Handles the Click event of the menuItem4 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void menuItem4_Click(object sender, EventArgs e)
        {
            Welcome.formBuffer.Clear();
        }
    }
}